using System;
using System.Collections.Generic;
using System.Text;

namespace ScokTcpLibB
{
    class CommonTools
    {
        public static byte CalcCheck(byte[] bts, int len)
        {
            byte chk = 0;
            for (int i = 0; i < len-2 && i<bts.Length-4; i++)
            {
                chk += bts[i + 4];
            }
            return chk;
        }
        public static byte[] Uint16ToByte(ushort tt)
        { 
            byte []m=new byte[2];
            m[0]=(byte)(tt>>8);
            m[1]=(byte)tt;
            return m;
        }
        public static ushort ByteToUshort(byte[] bts, int index)
        {
            return (ushort)(bts[index] << 8 | bts[index + 1]);
        }
        public static string bts2str(byte[] bts, int begin, int len)
        {
            string str = "";
            for (int i = 0; i < len && i + begin < bts.Length; i++)
            {
                str += string.Format("{0:X2}", bts[i + begin]);
            }
            return str;
        }

        public static string bts2str(byte[] bts,int len)
        {

            string str="";
            for (int i=0;i<len && i<bts.Length;i++)
            {
                str += string.Format("{0:X2} ", bts[i]);
            }
            return str;
        }
        public static int MakeDBQOrderAddr(ushort th,ushort bh)
        {
            return ((th << 16) | bh);
        }
        public static int MAKEDWORD(ushort lw, ushort hw)
        {
            return (((hw) << 16) | lw);
        }
        public static ushort LOWORD(int dword)
        {
            return (ushort)dword;
        }
        public static ushort HIWORD(int dword)
        {
            return (ushort)(dword >> 16);
        }
        public static int getBCDintValue(byte[] bts, int begin, int len)
        {
            string str = "";
            for (int i = 0; i < len && i < bts.Length; i++)
            {
                str += string.Format("{0:X2}", bts[begin + i]);
            }
            int res = 0;
            try
            {
                
                res= Convert.ToInt32(str);
            }
            catch
            {
                res= 0;
            }

            return res;
        }

        public static object getBCDValue(byte[] bts, int startindex, int valueindex, int len)
        {
            string str = "";
            for (int i = 0; i < len; i++)
            {
                str = string.Format("{0:X2}", bts[startindex + valueindex + i]) + str;
            }
            return str;
        }
        public static byte[] hexStringToBCD(string asc)
        {
            int len = asc.Length;
            int mod = len % 2;

            if (mod != 0)
            {
                asc = "0" + asc;
                len = asc.Length;
            }

            byte[] abt = new byte[len];
            if (len >= 2)
            {
                len = len / 2;
            }

            byte[] bbt = new byte[len];
            abt = System.Text.Encoding.ASCII.GetBytes(asc);
            int j, k;

            for (int p = 0; p < asc.Length / 2; p++)
            {
                if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9'))
                {
                    j = abt[2 * p] - '0';
                }
                else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z'))
                {
                    j = abt[2 * p] - 'a' + 0x0a;
                }
                else
                {
                    j = abt[2 * p] - 'A' + 0x0a;
                }

                if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9'))
                {
                    k = abt[2 * p + 1] - '0';
                }
                else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z'))
                {
                    k = abt[2 * p + 1] - 'a' + 0x0a;
                }
                else
                {
                    k = abt[2 * p + 1] - 'A' + 0x0a;
                }

                int a = (j << 4) + k;
                byte b = (byte)a;
                bbt[p] = b;
            }
            return bbt;  
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="fvalue"></param>
        /// <returns></returns>
        public static string getFdatetime(UInt32 fvalue)
        {
            int year = 0;
            int year100 = 0;
            int month = 0;
            int day = 0;
            int minute = 0;
            int hour = 0;

            minute = (Byte)(fvalue & 0x0000003F);
            hour = (Byte)((fvalue >> 8) & 0x00001F);
            day = (Byte)((fvalue >> 16) & 0x001F);
            month = (Byte)((fvalue >> 24) & 0x0F);
            year = (Byte)(((((fvalue >> 28) & 0x0F) << 3) | (fvalue >> 21) & 0x07));
            year100 = (Byte)((fvalue >> 13) & 0x0003);
            //if (year<=80)
            //{
            //year = 2000 + year100 * 100 + year;
            //year = 2000 + year;
            //}
            //else
            //{
            //    year = 1900 + year100 * 100 + year;
            //}

            return year + "-" + month + "-" + day + " " + hour + ":" + minute;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fvalue"></param>
        /// <returns></returns>
        public static string getGdatetime(UInt16 fvalue)
        {
            int year = 0;
            int month = 0;
            int day = 0;
            day = (fvalue & 0x001F);
            month = ((fvalue >> 8) & 0x0F);
            year = ((fvalue >> 9) & 0xF8) | ((fvalue >> 5) & 0x07);

            //if (year<=80)
            //{
            //    year = 2000 + year;
            //}
            return year + "-" + month + "-" + day;
        }

        public static string getSimCode(byte[] bts,int index)
        {
            return string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}", 
                bts[index + 0], bts[index + 1], bts[index + 2], 
                bts[index + 3], bts[index + 4], bts[index + 5]);
        }
    }
}
